/*package com.neo.shiro.session;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.session.mgt.ValidatingSession;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import com.neo.utils.SerializeUtils;

public class CachingShiroSessionDao extends CachingSessionDAO{
	
	private Logger logger = LoggerFactory.getLogger(CachingShiroSessionDao.class);
	
	private String prefix = "SHIROSESSION";
	
	public static final String UPDATE_REDIS_SESSION = CachingShiroSessionDao.class.getName() + "_UPDATE_REDIS_SESSION"; 
	
	private int seconds = 1800;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	public CachingShiroSessionDao() {
		super();
		logger.info("==>class CachingShiroSessionDao init()");
	}
	
//	@Override
//	public Session readSession(Serializable sessionId) {
//		logger.info("==>readSession session id [{}]", sessionId);
//		
//		String key = prefix + sessionId.toString();
//		String value = stringRedisTemplate.opsForValue().get(key);
//		SimpleSession simpleSession = (SimpleSession) SerializeUtils.dserializeToObject(value);
//		SimpleSession simpleSession = (SimpleSession) getCachedSession(sessionId);
//		logger.info("==>readSession simpleSession 状态 [{}]", simpleSession);
//		if (simpleSession != null && simpleSession.getAttributeKeys() != null) {
//			for (Object object : simpleSession.getAttributeKeys()) {
//				logger.info("[readSession AttributeKeys]"+object);
//			}
//		}else {
//			logger.info("[readSession AttributeKeys] null");
//		}
//		if (simpleSession instanceof Session) {
//			logger.info("==>session instanceof Session"+simpleSession.getLastAccessTime().toString());
//		}
//		if (simpleSession != null) {
//			logger.info("==>[readSession StartTimestamp]"+simpleSession.getStartTimestamp());
//		return simpleSession;
//		}
//		return null;
//	}
	
	@Override
	protected Session doReadSession(Serializable sessionId) {
		String key =  prefix + sessionId.toString();
		logger.info("==>获取Session key:{}", key);
		String value = stringRedisTemplate.opsForValue().get(key);
		Session session = SerializeUtils.dserializeToObject(value);
		if (!StringUtils.isEmpty(session)) {
//			session.setAttribute(UPDATE_REDIS_SESSION, true);
			stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);
			session.setTimeout(1800000);
			logger.info("==>获取session 状态1 [{}]", session);
		}else {
			logger.info("==>获取session 状态2 [{}]", session);
		}
		return session;
	}
	
	public Session getRedisSession(Serializable sessionId){
		String value = stringRedisTemplate.opsForValue().get(prefix + sessionId);
		Session session = SerializeUtils.dserializeToObject(value);
		return session;
	}
	
	@Override
	protected Serializable doCreate(Session session) {
		Serializable sessionId = this.generateSessionId(session);
		assignSessionId(session, sessionId);
//		SimpleSession simpleSession = (SimpleSession)session;
//		simpleSession.setId(sessionId);
//		simpleSession.setAttribute(UPDATE_REDIS_SESSION, true);
		logger.info("==>创建 session [{}]", session);
//		String value = SerializeUtils.serializeToString(simpleSession);
//		stringRedisTemplate.opsForValue().set(prefix + sessionId, value, seconds, TimeUnit.SECONDS);
		
		if (session.getAttributeKeys() != null) {
			for (Object object : session.getAttributeKeys()) {
				logger.info("[doCreate AttributeKeys] key:[{}] value:[{}]", object, session.getAttribute(object));
			}
		}else {
			logger.info("[doCreate AttributeKeys] null");
		}
		return sessionId;
	}
	
	@Override
	protected void doUpdate(Session session) {
		
		if (session.getAttributeKeys() != null) {
			for (Object object : session.getAttributeKeys()) {
				logger.info("[doUpdate AttributeKeys] key:[{}] value:[{}]", object, session.getAttribute(object));
			}
		}else {
			logger.info("[doUpdate AttributeKeys] null");
		}
		if ((boolean) session.getAttribute(UPDATE_REDIS_SESSION)
			&& session.getAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY) != null
			&& session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) != null){
    		String value = SerializeUtils.serializeToString((SimpleSession)session);
            stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
            session.setAttribute(UPDATE_REDIS_SESSION, false);
            logger.info("==>更新update redis session, session id [{}]", session.getId());
		}
		
		if (session instanceof ValidatingSession && ((ValidatingSession) session).isValid()) {
			logger.info("==>更新session validated return, session id [{}]", session.getId());
			return;
		}
		
		String key = prefix + session.getId().toString();
		logger.info("==>更新 key:{}", key);
//        if (!stringRedisTemplate.hasKey(key)) {
        	SimpleSession simpleSession = (SimpleSession)session;
    		String value = SerializeUtils.serializeToString(simpleSession);
            stringRedisTemplate.opsForValue().set(key, value);
            logger.info("==>更新redis session key:[{}]", key);
//        }
        stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);
	}
	
	@Override
	protected void doDelete(Session session) {
		Serializable sessionId = this.generateSessionId(session);
		String key = prefix + sessionId.toString();
		stringRedisTemplate.delete(key);
		logger.info("==>删除Session:[{}]", session.getId());
	}

	@Override
	protected void uncache(Session session) {
		super.uncache(session);
		logger.info("==>清除缓存session:[{}]", session.getId());
	}
	
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public void setSeconds(int seconds) {
		this.seconds = seconds;
	}
	
	
	[doUpdate AttributeKeys]org.apache.shiro.subject.support.DefaultSubjectContext_AUTHENTICATED_SESSION_KEY
	[doUpdate AttributeKeys]org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY
	
}
*/